import {GenData, GenDataDetail, GenParams} from "@/pages/system/tool/gen/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";


export const genPageList = async (genParams: GenParams) => {
  return request<{
    data: {
      records: GenData[],
      total: number,
    }
    success: boolean
  }>('/api/generate/list', {
    method: 'POST',
    data: genParams
  });
}


export const genUpdate = async (genDetail: GenDataDetail) => {
  return request<{
    data: string;
  }>('/api/generate/editGenTable', {
    method: 'POST',
    data: genDetail
  });
}

export const genRemove = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/generate/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const genDetail = async (id: number) => {
  return request<{
    data: GenDataDetail;
  }>('/api/generate/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}
export const getDatabaseTableList = async (data: any) => {
  return request<{
    data: {
      records: [],
      total: number,
    };
  }>('/api/generate/databaseTableList', {
    method: 'POST',
    data
  });
}
export const importTable = async (commonList: string[]) => {
  return request<{
    data: {
      records: [],
      total: number,
    }
  }>('/api/generate/importTable', {
    method: 'POST',
    data: {
      commonList
    }
  });
}


export const genImport = async (formData: FormData) => {
  return request<{
    data: string;
  }>('/api/generate/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const genExport = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/generate/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}


export const genAsync = async (commonList: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/generate/syncTable', {
    method: 'POST',
    data: {commonList},
  });
}

export const downloadGenExport = async (ids: (number | string)[]) => {
  await genExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const genExportTemplate = async () => {
  return request<{
    data: string;
  }>('/api/generate/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadGenExportTemplate = async () => {
  await genExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}


const getDownloadCode = async (ids: number[]) => {
  return request<{
    data: string;
  }>('/api/generate/downloadCode', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadCode = async (ids: number[]) => {
  await getDownloadCode(ids).then(({data, response}) => {
    download(data, response)
  });
}


export const previewCode = async (ids: number[]) => {
  return request<{
    data: []
  }>('/api/generate/previewCode', {
    method: 'POST',
    data: {ids},
  });
}
export const createEnum = async (id: number) => {
  return request<{
    data: []
  }>('/api/generate/createEnum', {
    method: 'POST',
    data: {id},
  });
}
export const genFieldTypeList = async (name: string) => {
  return request<{
    data: {
      name: string,
      id: number
    }[]
  }>('/api/generate/genFieldTypeList', {
    method: 'POST',
    data: {common: name},
  });
}

 const generateDictCode = async (data: { id: number, download: boolean, groupId: boolean }) => {
  return request<{}>('/api/generate/generateDictCode', {
    method: 'POST',
    data,
    responseType: 'blob',
    getResponse: true,
  });
}
export const downloadDictCode = async (d: { id: number, download: boolean, groupId: string, enum_: boolean}) => {
  // @ts-ignore
  await generateDictCode(d).then(({data, response}) => {
    if (d.download){
      download(data, response)
    }
  });
}

const generateConfigCode = async (data: { id: number, download: boolean, groupId: boolean }) => {
  return request<{}>('/api/generate/generateConfigCode', {
    method: 'POST',
    data,
    responseType: 'blob',
    getResponse: true,
  });
}
export const downloadConfigCode = async (d: { id: number, download: boolean, groupId: boolean }) => {
  await generateConfigCode(d).then(({data, response}) => {
    download(data, response)
  });
}

